package com.wxp.demo03.button;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo03.R;
import com.wxp.demo03.util.DateUtils;

/**
 * 4.3禁用与恢复按钮
 */
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_test;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setTitle("4.3禁用与恢复按钮");
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_button_enable);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.button_enable), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //获取组件
        btn_test = findViewById(R.id.btn_test);
        tv_result = findViewById(R.id.tv_result);
        //设置按钮事件
        Button btn_enable = findViewById(R.id.btn_enable);
        Button btn_disable = findViewById(R.id.btn_disable);
        //为了方便复用都使用自己的实现的点击事件
        btn_enable.setOnClickListener(this);
        btn_disable.setOnClickListener(this);
        btn_test.setOnClickListener(this);
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        //判断按钮
        if (v.getId() == R.id.btn_enable) {
            // 启用当前控件
            btn_test.setEnabled(true);
            // 设置按钮的文字颜色
            btn_test.setTextColor(Color.BLACK);
        } else if (v.getId() == R.id.btn_disable) {
            // 禁用当前控件
            btn_test.setEnabled(false);
            btn_test.setTextColor(Color.GRAY);
        } else if (v.getId() == R.id.btn_test) {
            String desc = String.format("%s 您点击了按钮： %s", DateUtils.getNowTime(), ((Button) v).getText());
            tv_result.setText(desc);
        }
    }
}